package com.jl.smartcard.core.actions;

import java.nio.charset.Charset;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.KeyAdapter;
import org.eclipse.swt.events.KeyEvent;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Dialog;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;
import org.eclipse.wb.swt.SWTResourceManager;

import com.jl.card.ISecurityOperation;
import com.jl.util.HexUtil;

public class VerifyDialogUI extends Dialog {

	private String[] items;
	private ISecurityOperation se;
	private Display display;
	protected Object result;
	protected Shell shlVerifyDialog;
	private Text textValue;
	private Text textNewValue;
	private Text textConfirm;
	private Combo comboCommand;
	private Combo comboType;
	private Combo comboCard;
	private Label lblTip;
	private Button btnHexMode;

	/**
	 * Create the dialog.
	 * 
	 * @param parent
	 * @param style
	 */
	public VerifyDialogUI(Shell parent, int style) {
		super(parent, style);
		setText("Verify Dialog");
		createContents();
	}

	/**
	 * Open the dialog.
	 * 
	 * @return the result
	 */
	public Object open() {
		display = getParent().getDisplay();
		Rectangle displayBounds = display.getPrimaryMonitor().getBounds();
		Rectangle shellBounds = shlVerifyDialog.getBounds();
		int x = displayBounds.x + (displayBounds.width - shellBounds.width) >> 1;
		int y = displayBounds.y + (displayBounds.height - shellBounds.height) >> 1;
		shlVerifyDialog.setLocation(x, y);

		shlVerifyDialog.open();
		shlVerifyDialog.layout();

		while (!shlVerifyDialog.isDisposed()) {
			if (!display.readAndDispatch()) {
				display.sleep();
			}
		}
		return result;
	}

	/**
	 * Create contents of the dialog.
	 */
	private void createContents() {
		shlVerifyDialog = new Shell(getParent(), SWT.BORDER | SWT.TITLE);
		shlVerifyDialog.setFont(SWTResourceManager.getFont("Courier New", 11,
				SWT.NORMAL));
		shlVerifyDialog.setSize(313, 276);
		shlVerifyDialog.setText("Verify Dialog");
		shlVerifyDialog.setLayout(new GridLayout(4, false));

		comboCard = new Combo(shlVerifyDialog, SWT.READ_ONLY);
		comboCard.setFont(SWTResourceManager.getFont("Courier New", 10,
				SWT.NORMAL));
		comboCard.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false,
				4, 1));
		new Label(shlVerifyDialog, SWT.NONE);
		new Label(shlVerifyDialog, SWT.NONE);
		new Label(shlVerifyDialog, SWT.NONE);
		new Label(shlVerifyDialog, SWT.NONE);

		Label lblCommand = new Label(shlVerifyDialog, SWT.NONE);
		lblCommand.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false,
				false, 1, 1));
		lblCommand.setFont(SWTResourceManager.getFont("Courier New", 10,
				SWT.NORMAL));
		lblCommand.setText("Command ");

		comboCommand = new Combo(shlVerifyDialog, SWT.READ_ONLY);
		comboCommand.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				int index = comboCommand.getSelectionIndex();
				if (index == 1 || index == 4) {
					textNewValue.setEnabled(true);
					textConfirm.setEnabled(true);
				} else {
					textNewValue.setEnabled(false);
					textConfirm.setEnabled(false);
				}
			}
		});
		comboCommand.setItems(new String[] { "Verify", "Change", "Enable",
				"Disable", "Unblock" });
		comboCommand.setFont(SWTResourceManager.getFont("Courier New", 10,
				SWT.NORMAL));
		comboCommand.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false,
				false, 1, 1));
		comboCommand.select(0);

		Label lblType = new Label(shlVerifyDialog, SWT.NONE);
		lblType.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false,
				1, 1));
		lblType.setFont(SWTResourceManager.getFont("Courier New", 10,
				SWT.NORMAL));
		lblType.setText("Type");

		comboType = new Combo(shlVerifyDialog, SWT.READ_ONLY);
		comboType.setItems(new String[] {});
		comboType.setFont(SWTResourceManager.getFont("Courier New", 10,
				SWT.NORMAL));
		comboType.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false,
				1, 1));
		comboType.select(0);
		new Label(shlVerifyDialog, SWT.NONE);
		new Label(shlVerifyDialog, SWT.NONE);
		new Label(shlVerifyDialog, SWT.NONE);

		btnHexMode = new Button(shlVerifyDialog, SWT.CHECK);
		btnHexMode.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				textConfirm.setText("");
				textNewValue.setText("");
				textValue.setText("");
			}
		});
		btnHexMode.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false,
				false, 1, 1));
		btnHexMode.setFont(SWTResourceManager.getFont("Courier New", 10,
				SWT.NORMAL));
		btnHexMode.setText("HEX Mode");

		Label lblValue = new Label(shlVerifyDialog, SWT.NONE);
		lblValue.setFont(SWTResourceManager.getFont("Courier New", 10,
				SWT.NORMAL));
		lblValue.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false,
				false, 1, 1));
		lblValue.setText("Value");

		textValue = new Text(shlVerifyDialog, SWT.BORDER);
		textValue.addKeyListener(new KeyAdapter() {
			@Override
			public void keyPressed(KeyEvent e) {
				valueKeyPressed(e);
			}
		});
		textValue.setFont(SWTResourceManager.getFont("Courier New", 10,
				SWT.NORMAL));
		textValue.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false,
				3, 1));

		Label lblNewValue = new Label(shlVerifyDialog, SWT.NONE);
		lblNewValue.setFont(SWTResourceManager.getFont("Courier New", 10,
				SWT.NORMAL));
		lblNewValue.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false,
				false, 1, 1));
		lblNewValue.setText("New Value");

		textNewValue = new Text(shlVerifyDialog, SWT.BORDER);
		textNewValue.addKeyListener(new KeyAdapter() {
			@Override
			public void keyPressed(KeyEvent e) {
				valueKeyPressed(e);
			}
		});
		textNewValue.setEnabled(false);
		textNewValue.setFont(SWTResourceManager.getFont("Courier New", 10,
				SWT.NORMAL));
		textNewValue.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true,
				false, 3, 1));

		Label lblConfirm = new Label(shlVerifyDialog, SWT.NONE);
		lblConfirm.setFont(SWTResourceManager.getFont("Courier New", 10,
				SWT.NORMAL));
		lblConfirm.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false,
				false, 1, 1));
		lblConfirm.setText("Confirm");

		textConfirm = new Text(shlVerifyDialog, SWT.BORDER);
		textConfirm.setEnabled(false);
		textConfirm.setFont(SWTResourceManager.getFont("Courier New", 10,
				SWT.NORMAL));
		textConfirm.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true,
				false, 3, 1));
		new Label(shlVerifyDialog, SWT.NONE);

		Button btnNewButton = new Button(shlVerifyDialog, SWT.NONE);
		btnNewButton.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				cancelPressed();
			}
		});
		btnNewButton.setFont(SWTResourceManager.getFont("Courier New", 10,
				SWT.NORMAL));
		GridData gd_btnNewButton = new GridData(SWT.LEFT, SWT.CENTER, false,
				false, 1, 1);
		gd_btnNewButton.widthHint = 69;
		btnNewButton.setLayoutData(gd_btnNewButton);
		btnNewButton.setText("Cancel");
		new Label(shlVerifyDialog, SWT.NONE);

		Button btnOk = new Button(shlVerifyDialog, SWT.NONE);
		btnOk.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				okPressed();
			}
		});
		GridData gd_btnOk = new GridData(SWT.LEFT, SWT.CENTER, false, false, 1,
				1);
		gd_btnOk.widthHint = 71;
		btnOk.setLayoutData(gd_btnOk);
		btnOk.setFont(SWTResourceManager.getFont("Courier New", 10, SWT.NORMAL));
		btnOk.setText("OK");

		Label lblNewLabel = new Label(shlVerifyDialog, SWT.NONE);
		lblNewLabel.setAlignment(SWT.RIGHT);
		lblNewLabel.setFont(SWTResourceManager.getFont("Courier New", 10,
				SWT.NORMAL));
		lblNewLabel.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false,
				true, 1, 1));
		lblNewLabel.setText("Info:");

		lblTip = new Label(shlVerifyDialog, SWT.NONE);
		lblTip.setFont(SWTResourceManager
				.getFont("Courier New", 10, SWT.NORMAL));
		lblTip.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false, false,
				3, 1));

	}

	protected void valueKeyPressed(KeyEvent e) {
		Text from = (Text) e.getSource();
		if (e.character == '\b') {
			return;
		}
		if (btnHexMode.getSelection()) {
			if (!HexUtil.isHex(e.character)) {
				e.doit = false;
			}
			if (from.getText().length() >= 16 && from.getSelectionCount() == 0) {
				e.doit = false;
			}
		} else {
			if (from.getText().length() >= 8 && from.getSelectionCount() == 0) {
				e.doit = false;
			}
		}
	}

	protected void okPressed() {
		String value = this.textValue.getText().trim();
		String newValue = this.textNewValue.getText().trim();
		String strType = this.comboType.getText();

		byte[] arrayValue = null;
		byte[] arrayNewValue = null;
		if (btnHexMode.getSelection()) {
			arrayValue = HexUtil.parseHexToByteArray(value);
			arrayNewValue = HexUtil.parseHexToByteArray(newValue);
		} else {
			arrayValue = value.getBytes(Charset.forName("US-ASCII"));
			arrayNewValue = newValue.getBytes(Charset.forName("US-ASCII"));
		}

		int type = -1;
		if (strType.equalsIgnoreCase("PIN1")) {
			type = 0x01;
		} else if (strType.equalsIgnoreCase("PIN2")) {
			type = 0x02;
		} else if (strType.equalsIgnoreCase("ADM1")) {
			type = 0x0a;
		}
		boolean ok = false;
		switch (this.comboCommand.getSelectionIndex()) {
		case 0:// verify
			ok = se.verify(type, arrayValue);
			break;
		case 1:// Change
			ok = se.change(type, arrayValue, arrayNewValue);
			break;
		case 2:// Enable
			ok = se.enable(type, arrayValue);
			break;
		case 3:// Disable
			ok = se.disable(type, arrayValue);
			break;
		case 4:// Unblock
			ok = se.unblock(type, arrayValue, arrayNewValue);
			break;
		default:
			break;
		}
		if (ok) {
			this.lblTip.setText("Success");
			shlVerifyDialog.close();
		} else {
			this.lblTip.setText("Failed");
		}

	}

	protected void cancelPressed() {
		shlVerifyDialog.close();
		result = Boolean.FALSE;
	}

	public void setCards(String[] allCardsName) {
		this.items = allCardsName;
		if (items == null) {
			return;
		}
		this.comboCard.setItems(this.items);
	}

	public void selectCard(String last) {
		for (int i = 0; i < items.length; i++) {
			if (items[i].equals(last)) {
				this.comboCard.select(i);
				break;
			}
		}
	}

	public void setSecurityOperation(ISecurityOperation op) {
		this.se = op;
		this.comboType.setItems(op.getAllTypes());
		this.comboType.select(0);
	}

}
